using System;
using System.Collections;
using ScenarioFramework.WorkflowEngine.DataModel;

namespace ScenarioFramework.WorkflowEngine.DataModel.WorkflowApplicationsData
{
	//*********************************************************************
	//
	// XMLApplications Class
	//
	// The XMLApplications class represents a collection base of XMLApplication
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLApplications Has no fields
	//
	//*********************************************************************
	public class XMLApplications : bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE = "Applications";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLApplications()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLApplications(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		public override void Init()
		{
			this.RemoveAll();
		}

		/**Method to parse an xml string into a nodelist collection **/
		public override void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLApplication.MAIN_NODE:
						this.Add ( new XMLApplication(node));
						break;
				}
			}
		}

		/** Method to return an application from collection **/
		public XMLApplication Get(string sName)
		{
			XMLApplication application;

			for ( int i=0; i<this.List.Count; i++)
			{
				application = (XMLApplication)this.List[i];
				if ( application.Name == sName )
				{
					return application;
				}
			}

			throw new Exception.ApplicationNotFound(sName);
		}

		/**Method to retive a nodelist from collection **/
		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLApplications.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name,
			Type,
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Type:
					InnerList.Sort(new TypeComparer());
					break;
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLApplication first = (XMLApplication) x;
				XMLApplication second = (XMLApplication) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class TypeComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLApplication first = (XMLApplication) x;
				XMLApplication second = (XMLApplication) y;
				return first.Type.CompareTo(second.Type);
			}
		}
	}
}